package com.qingxin.web.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.cms.entity.CmsArticle;
import com.qingxin.cms.entity.CmsCategory;
import com.qingxin.cms.service.CmsArticleService;
import com.qingxin.cms.service.CmsCategoryService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;

@Controller
public class NewsAction extends BaseAction {
	
	@Autowired
	private CmsCategoryService cmsCategoryService;
	
	@Autowired
	private CmsArticleService cmsArticleService;
	
	/**
	 * 质量措施
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/zlcs.do", method=RequestMethod.GET)
	public String zlcs(ModelMap model, HttpServletRequest request) {
		model.put("model", "index");
		model.put("type", "zlcs");
		model.put("typeName", "质量措施");
		
		return "cms/news_list";
	}
	
	
	/**
	 * 农业概况
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/nygk.do", method=RequestMethod.GET)
	public String index(ModelMap model, HttpServletRequest request) {
		model.put("model", "index");
		model.put("type", "nygk");
		model.put("typeName", "农业概况");
		return "cms/news_list";
	}
	
	
	/**
	 * 资讯列表数据
	 * @param type
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/listNews.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listPark(String type, PageParam pageParam, HttpServletRequest request){
		pageParam.setPagesize(10);
		Pager pager = cmsArticleService.list(type, pageParam, request);
		return pager;
	}
	
	
	/**
	 * 详情页
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/detail.do", method=RequestMethod.GET)
	public String index(ModelMap model, String id, HttpServletRequest request) {
		model.put("model", "index");
		if(id != null){
			CmsArticle article = cmsArticleService.get(id);
			model.put("article", article);
			
			if(article != null){
				CmsCategory category = cmsCategoryService.get(article.getCmsCategory().getCategoryId());
				model.put("type", category.getCategoryCode());
				model.put("typeName", category.getCategoryName());
			}
			
		}
		
		return "cms/news_detail";
	}
	
	
	
	
}
